package org.txazo.blog.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.txazo.blog.util.HttpUtil;
import org.txazo.blog.util.log.LoggerManager;

/**
 * PV日志过滤器
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
public class PVLogFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;

		String uri = request.getRequestURI();
		String url = request.getRequestURL().toString();

		/** 记录页面PV访问(IP | Method | URL | Refer) */
		if (HttpUtil.isDynamicRequestOfURI(uri)) {
			LoggerManager.getConsoleLogger().info(url);

			LoggerManager.getPVLogger().info(
					HttpUtil.getRemoteIp(request) + " " + request.getMethod()
							+ " "
							+ HttpUtil.getRequestURLExcludeParameters(url)
							+ " " + HttpUtil.getRequestRefer(request));
		}

		chain.doFilter(req, resp);
	}

	@Override
	public void destroy() {
	}

}
